﻿<!-- #include file="../Include/Admin_Onlogin.asp" -->
<%
Dim sqlAuth, rsAuth, PurviewID
sqlAuth = "Select AuthID From Admin_Auth Where href='Admin/Admin_Sql.asp' Order By sort Asc,AuthID Asc"
Call Exec(rsAuth,sqlAuth,2)
If rsAuth.BOF And rsAuth.EOF Then
	PurviewID = 0
Else
	PurviewID = rsAuth(0)
	CheckPurview PurviewID,0
End If
rsAuth.Close
Set rsAuth = Nothing
Dim SQL_Statement,RecordsAffected,RetRs,oField,Page_Count,TotalRec,abgcolor,CurPage,PCount,EndPage
Response.Write "<!DOCTYPE html>" & vbCrLf
Response.Write "<head>" & vbCrLf
Response.Write "<title>执行SQL</title>" & vbCrlf
Response.Write "<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>" & vbCrLf
Response.Write "<meta name='viewport' content='width=device-width, initial-scale=1, maximum-scale=1'>" & vbCrLf
Response.Write "<meta name='apple-mobile-web-app-status-bar-style' content='black'>" & vbCrLf
Response.Write "<meta name='apple-mobile-web-app-capable' content='yes'>" & vbCrLf
Response.Write "<meta name='format-detection' content='telephone=no'>" & vbCrLf
Response.Write "<link href='../Plugins/layui/css/layui.css' rel='stylesheet' type='text/css' />" & vbCrLf
Response.Write "<link rel=""stylesheet"" href=""../Style/global.css"" media=""all"">" & vbCrLf
Response.Write "<link rel=""stylesheet"" href=""../Style/font.css"" media=""all"">" & vbCrLf
Response.Write "<script type='text/javascript'>" & vbCrLf
Response.Write "var pc_file = '" & InstallDir & "';" & vbCrLf
Response.Write "var admin_file = '" & AdminFolder & "/';" & vbCrLf
Response.Write "</script>" & vbCrLf
Response.Write "<script type='text/javascript' src='../Js/Admin.js'></script>" & vbCrLf
Response.Write "<script type='text/javascript' src='../../Dialog/main.js'></script>" & vbCrLf
Response.Write "<script type='text/javascript' src='../../Dialog/lhgcore.min.js'></script>" & vbCrLf
Response.Write "<script type='text/javascript' src='../../Dialog/lhgcalendar.min.js'></script>" & vbCrLf
Response.Write "</head>" & vbCrlf
Response.Write "<body>" & vbCrlf
Response.Write "<div class=""admin-main layui-anim layui-anim-upbit"">" & vbCrLf
Response.Write "<div class=""layui-row layui-col-space10"">" & vbCrLf
Response.Write "<blockquote class=""layui-elem-quote title"">SQL语句执行操作说明！</blockquote>" & vbCrLf
Response.Write "<table class=""layui-table magt0"">" & vbCrlf
Response.Write "<tr>" & vbCrlf
Response.Write "<TD><br>" & vbCrlf
Response.Write "<ul type=1 style=""line-height:200%;"">" & vbCrlf
Response.Write "<li>本操作仅限高级、对SQL编程比较熟悉的用户！</li>" & vbCrlf
Response.Write "<li>您可以直接输入SQL执行语句，比如〖 select * from art 〗查询友情链接信息。</li>" & vbCrlf
Response.Write "<li>数据记录筛选：select * from 数据表 [ where 字段名=字段值 order by 字段名 ]</li>" & vbCrlf
Response.Write "<li>更新数据记录：update 数据表 set 字段名1=字段值1,……字段名n=字段值n [where 条件表达式]</li>" & vbCrlf
Response.Write "<li>删除数据记录：delete from 数据表 [where 条件表达式]</li>" & vbCrlf
Response.Write "<li>添加数据记录：insert into 数据表 (字段1，字段2，字段3……) values (值1，值2，值3……)</li>" & vbCrlf
Response.Write "<li>删除数据库表格：drop table 命令删除某个表格以及该表格中的所有记录</li>" & vbCrlf
Response.Write "<li>添加数据库表格：create table 命令添加某个表格以及该表格中的所有记录</li>" & vbCrlf
Response.Write "</ul></td></tr></table>" & vbCrLf
Response.Write "</div>" & vbCrLf
Response.Write "<br>" & vbCrlf
Response.Write "<div class=""layui-row layui-col-space10"">" & vbCrLf
Response.Write "<table class=""layui-table magt0"">" & vbCrlf
If Action = "sql_done" Then
	sqlAuth = "Select AuthID From Admin_Auth Where pid="&PurviewID&" And href='Sql/Update' Order By sort Asc,AuthID Asc"
	Call Exec(rsAuth,sqlAuth,2)
	If rsAuth.BOF And rsAuth.EOF Then
		CheckPurview 0,0
	Else
		CheckPurview rsAuth(0),0
	End If
	rsAuth.Close
	Set rsAuth = Nothing
	SQL_Statement=Request("SQL_Statement")
	If SQL_Statement="" Then
		Response.write "<tr><td align=center>请至少输入一句SQL语句！<br><br><button type=""button"" class=""layui-btn"" hidefocus=""true"" onClick=""javascript:history.back();"">返回上一页</button></td></tr></table></div>" & vbCrLf
	End If
	If SQL_Statement<>"" Then
		On Error Resume Next
		Server.ScriptTimeOut=10
		Set RetRs=Conn.Execute(SQL_Statement,RecordsAffected)
		If Err.number="0" Then
			On Error Goto 0
			Response.Write "<blockquote class=""layui-elem-quote title"">SQL语句执行操作说明！</blockquote>" & vbCrLf
			If RetRs.State=0 Then
				If RecordsAffected>0 Then
					Response.write "<tr><td align=center><span>执行成功，共有 "&RecordsAffected&" 条记录被处理。<span><br><br><button type=""button"" class=""layui-btn"" hidefocus=""true"" onClick=""javascript:history.back();"">返回上一页</button></td></tr></Table></div>" & vbCrLf
				Else
					Response.write "<tr><td align=center>执行成功，没有任何记录被处理。<br><br><button type=""button"" class=""layui-btn"" hidefocus=""true"" onClick=""javascript:history.back();"">返回上一页</button></td></tr></Table></div>" & vbCrLf
				End If
			Else
				RetRs.Close
				RetRs.Open SQL_Statement,Conn,1,1
				If RetRs.Bof And RetRs.Eof Then
					Response.write "<tr><td align=center>执行成功，没有任何记录被返回。<br><br><button type=""button"" class=""layui-btn"" hidefocus=""true"" onClick=""javascript:history.back();"">返回上一页</button></td></tr></Table></div>" & vbCrLf
				Else
					CurPage=request("page")
					If CurPage="" or not IsNumeric(CurPage) Then
						CurPage=1
					Else
						CurPage=CLng(CurPage)
					End If
					'删除语句
					If RetRs.RecordCount>0 Then
						RetRs.AbsolutePage=CurPage
						Page_Count=0
						TotalRec=RetRs.RecordCount
						Response.write "<tr><td align=center><span>执行成功，共返回 "&TotalRec&" 条记录。</span><br><br><button type=""button"" class=""layui-btn"" hidefocus=""true"" onClick=""javascript:history.back();"">返回上一页</button></td></tr></Table></div>" & vbCrLf
						Response.Write "<br>"
						Response.Write "<div class=""layui-row layui-col-space10"">" & vbCrLf
						Response.Write "<table class=""layui-table magt0""><tr class=title>"
						For Each oField In RetRs.Fields
							Response.Write "<Td align=center><b>" & oField.Name &"</b></Td>"
						Next
						Do While (not RetRs.eof) And (not Page_Count = Cint(RetRs.PageSize))
							Response.Write "<tr>"
							If Page_Count mod 2 = 0 Then
								abgcolor="ForumRow"
							Else
								abgcolor="ForumRowHilight"
							End If
							For i=0 To RetRs.Fields.Count-1
								Response.write "<td align=center height=25>"
								If IsNull(RetRs(i)) Then
									Response.Write ""
								Else
									Response.Write Server.HTMLEncode(RetRs(i))
								End If
								Response.write "</td>"
							Next
							Response.Write "</tr>"
							Page_count = Page_Count + 1
							RetRs.MoveNext
						Loop
						Response.Write "<tr><td colspan="&RetRs.Fields.Count&" class=forumrow align=center>分页："
						Pcount=RetRs.PageCount
						If CurPage > 4 Then
							Response.write "<a href=""?page=1&Action=sql_done&sql_statement="&SQL_Statement&""">[1]</a> ..."
						End If
						If PCount>CurPage+3 Then
							EndPage=CurPage+3
						Else
							EndPage=PCount
						End If
						For i=CurPage-3 To EndPage
							If i>=1 Then
								If i = CLng(CurPage) Then
									Response.write " <font color=red>["&i&"]</font>"
								Else
									Response.write " <a href=""?page="&i&"&Action=sql_done&sql_statement="&SQL_Statement&""">["&i&"]</a>"
								End If
							End If
						Next
						If CurPage+3 < PCount Then 
							Response.write "... <a href=""?page="&PCount&"&Action=sql_done&sql_statement="&SQL_Statement&""">["&PCount&"]</a>"
						End If
					Else
						Response.write "<tr><td align=center>执行成功，返回如下记录：<br><br><button type=""button"" class=""layui-btn"" hidefocus=""true"" onClick=""javascript:history.back();"">返回上一页</button></td></tr></Table></div>"
						Response.Write "<br>" & vbCrLf
						Response.Write "<div class=""layui-row layui-col-space10"">" & vbCrLf
						Response.Write "<table class=""layui-table magt0""><tr>"
						For Each oField In RetRs.Fields
							If oField.Name<>"" Then
								Response.Write "<Th>" & oField.Name &"</Th>"
							Else
								Response.Write "<Td>执行结果：</Td>"
							End If
						Next
						Do While not RetRs.eof
							Response.Write "<tr>"
							If Page_Count mod 2 = 0 Then
								abgcolor="ForumRow"
							Else
								abgcolor="ForumRowHilight"
							End If
							For i=0 To RetRs.Fields.Count-1
								Response.write "<td>"
								If IsNull(RetRs(i)) Then
									Response.Write ""
								Else
									Response.Write RetRs(i)
								End If
								Response.write "</td>"
							Next
							Response.Write "</tr>"
							RetRs.MoveNext
						Loop
					End If
					Response.Write "</table></td></tr></Table>"
					Response.Write "</div>" & vbCrLf
				End If
				RetRs.Close
			End If
		Else
			Response.Write "<blockquote class=""layui-elem-quote title"">执行结果！</blockquote>" & vbCrLf
			Response.write "<td align=center colspan=2>SQL语句有问题，具体错误信息如下：<br>"&Err.Description&"<br><br><button type=""button"" class=""layui-btn"" hidefocus=""true"" onClick=""javascript:history.back();"">返回上一页</button></td></tr></Table></div>"
			Err.clear
		End if
		On Error Goto 0
		Set RetRs=Nothing
	End If
Else
	Response.Write "</Table></div>" & vbCrlf
	Response.Write "<script type=""text/javascript"">" & vbCrlf
	Response.Write "function CheckForm() {" & vbCrlf
	Response.Write "if(document.myform.SQL_Statement.value=="""") {" & vbCrlf
	Response.Write "IsAlertFocus('操作语句不能为空！','SQL_Statement');" & vbCrlf
	Response.Write "return false;" & vbCrlf
	Response.Write "}" & vbCrlf
	Response.Write "return confirm(""您真的要执行吗？"");" & vbCrlf
	Response.Write "}</script>" & vbCrlf
	Response.Write "<blockquote class=""layui-elem-quote title"">执行SQL</blockquote>" & vbCrLf
	Response.Write "<table class=""layui-table magt0"">" & vbCrlf
	Response.Write "<tr>" & vbCrlf
	Response.Write "<td align=""center""><form name=""myform"" method=""post"" Action=""?Action=sql_done"">" & vbCrlf
	Response.Write "<textarea id=""SQL_Statement"" name=""SQL_Statement"" class=""layui-textarea""></textarea>" & vbCrlf
	Response.Write "<br><br>" & vbCrlf
	Response.Write "<button type=""submit"" class=""layui-btn"" hidefocus=""true"" onClick='return CheckForm();'> 执  行 </button>" & vbCrlf
	Response.Write "</form></td>" & vbCrlf
	Response.Write "</tr>" & vbCrlf
	Response.Write "</table>" & vbCrlf
End If
Response.Write "</div>" & vbCrLf
Response.Write "</body>" & vbCrlf
Response.Write "</html>"
%>